# coding=utf-8
"""
Subtitle Decoder
Uses some library files from
http://xbmc-addon-repository.googlecode.com
Thanks!
"""
import sys
import os
import pdb
from os import path, access, W_OK
from urlparse import urlparse
from BeautifulSoup import BeautifulSoup
from crunchyDec import crunchyDec
from crunchy_video import CrunchyPlayback


xmlSource = open(sys.argv[1])
xmlsoup = BeutifulSoup(xmlSource)
series_title = soup.find('series_title').string
episode_title = soup.find('episode_title').string
episode_number = soup.find('episode_number').string
title = series_title+' Episode '+episode_number+' - '+episode_title
try:
	formattedSubs = crunchyDec().returnSubs(xmlsoup)
except TypeError:
	sys.exit(title + ' has hardcoded subtitles.')
try:
	subfile = open(sys.argv[2], 'w')
except IndexError:
	subfile = open(title+'.ass', 'w')
subfile.write(formattedSubs.encode('utf-8-sig'))
subfile.close()